const express = require('express');
const router = express.Router();
const { success, failure } = require('../utils/response');
const {Category} = require("../models")
const {setKey,getKey} = require("../utils/redis")
const { NotFound } = require("http-errors");

// 查询所有分类
router.get("/",async(req,res)=>{
    try {
        const categories = await Category.findAll({order:[["rank","ASC"],["id","DESC"]]})
        success(res,"获取分类列表成功。",{categories})
    } catch (error) {
        failure(res, error);
    }
})

// 查询所有分类
router.get("/redis",async(req,res)=>{
    try {
        const cacheKey = 'category'

        let categories = await getKey(cacheKey)
        if(!categories){
            categories = await Category.findAll({order:[['rank','ASC'],['id','DESC']]})
            await setKey(cacheKey,categories)
        }
        success(res,'查询分类数据成功',{categories})
    } catch (error) {
        failure(res,error)
    }
})

module.exports = router;